package com.myl.service.system.impl;

import java.util.List;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myl.dao.system.ModuleDao;
import com.myl.dao.system.UserDao;
import com.myl.domain.system.Module;
import com.myl.domain.system.User;
import com.myl.factory.MapperFactory;
import com.myl.service.system.UserService;
import com.myl.utils.MD5Util;
import com.myl.utils.TransactionUtil;

public class UserServiceImp implements UserService{

	@Override
	public void save(User user) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			user.setId(id);
			//使用md5加密
			user.setPassword(MD5Util.md5(user.getPassword()));
			
			
			userDao.save(user);
			
			TransactionUtil.commit(sqlSession);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public void delete(User user) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			//id使用uuid的生成策略来获取

			userDao.delete(user);
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public void update(User user) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			
			//更新方案
			//1.读取数据库的信息，覆盖
			//.修改update的语句
			userDao.update(user);
			
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public User findById(String id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			return userDao.findById(id);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public List<User> findAll() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			
			List<User> all = userDao.findAll();
			return all;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public PageInfo findAll(int page, int size) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			
			PageHelper.startPage(page,size);
			List<User> all = userDao.findAll();
			PageInfo  pageInfo = new PageInfo(all);
			return pageInfo;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public void updateRole(String userId, String[] roleIds) {
		
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			
			//先删除原来的关系，再覆盖现有的关系
			userDao.deleteRole(userId);
			for(String roleId:roleIds){
				userDao.updateRole(userId,roleId);
			}
			
			TransactionUtil.commit(sqlSession);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public User login(String email, String pwd) {
		
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			UserDao userDao = MapperFactory.getMapper(sqlSession, UserDao.class);		
			
			pwd = MD5Util.md5(pwd);
			return userDao.findByEmailAndPwd(email,pwd);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public List<Module> findModuleById(String id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);		
			
			
			return moduleDao.findModuleById(id);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}
		
	}



